var suggest = require('../../../../public/suggest');
var _data = require('{pub}/data');
var auxiliary = require('./auxiliary');

return function(elem){
    var that = this;
    elem.suggest(extend(suggest, {
        zindex:that.layer ? that.layer._zIndex : null,
        scrolls:[elem.closest('.voucher-content')],
        theme:'suggest-subject',
        offset:{
            left:-9,
            top:6,
            bottom:5,
            width:50
        },
        returnData:function(data, index){
            return '<li data-index='+index+'>'+ data.buname +'</li>';
        },
        diyCallback:function(){
            return '<a href="javascript:void(0);" class="add-subject"><i class="iconfont">&#xe64c;</i> 新增科目</a>';
        },
        getData:function(keywords){
            var data = _data.get('subject');
            if(keywords === true){
                return data
            }
            var list = [], kw = Nui.trim(keywords);
            Nui.each(data, function(val, key){
                if(val.buname.indexOf(kw) !==- 1){
                    list.push(data[key]);
                }
            });
            return list;
        },
        callback:function(ele){
            
        },
        eventEnd:function(item, ele){
            var data = _data.get('subject', item.data('index'));
            //辅助核算
            if(data.auxiliary && data.auxiliary.length){
                auxiliary.call(that, ele, data)
            }
            else{
                that.nextFocus(null, ele);
            }
        }
    }))
}